package cn.wolf.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 自定义协议TCP - 编码处理器
 */
public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {
    /**
     *  编码处理
     * @param ctx           the {@link ChannelHandlerContext} which this {@link MessageToByteEncoder} belongs to
     * @param msg           the message to encode
     * @param out           the {@link ByteBuf} into which the encoded message will be written
     * @throws Exception 异常
     */
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("MyMessageEncoder encoder 方法被调用!");
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }
}
